package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.User;
import com.ynsj.domain.UserBank;
import com.ynsj.model.R;
import com.ynsj.service.UserBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(tags = "会员银行卡管理")
@RequestMapping("/userBanks")
public class UserBankController {

    @Autowired
    private UserBankService userBankService;

    @GetMapping
    @ApiOperation("分页查询会员银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "userId",value = "会员Id,精确查询")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> findByPage(@ApiIgnore Page<UserBank> page, Long usrId){
        return R.ok(userBankService.findBankByPage(page,usrId));
    }

    @PatchMapping
    @ApiOperation("修改会员银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank",value = "会员银行卡信息json")
    })
    public R updateUserBank(@RequestBody @Validated UserBank userBank){
        boolean isOk = userBankService.updateById(userBank);
        if (isOk){
            return R.ok("更新会员银行卡信息成功");
        }
        return R.fail("更新会员银行卡信息失败");
    }

    @PostMapping("/status")
    @ApiOperation("启用/禁用银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "会员的id"),
            @ApiImplicitParam(name = "status",value = "会员银行卡状态")
    })
    public R updateStatus(Long id,Integer status){
        boolean isOk = userBankService.updateStatus(id,status);
        if (isOk){
            return R.ok("启用/禁用银行卡信息成功");
        }
        return R.fail("启用/禁用银行卡信息失败");
    }
}
